﻿/* ==============================================================================
* 功能描述：SyncMsgHandler
* 创 建 者：青金石
* 创建日期：2020/7/7 20:39:55
* ==============================================================================*/

using System;
using TCPServer.script.net;

public partial class MsgHandler
{
    //收到MsgSyncHero协议
    public static void MsgSyncHero(ClientState state, MsgBase msgBase)
    {
        MsgSyncHero msg = (MsgSyncHero)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        Room room = RoomManager.GetRoom(player.roomID);
        if (room == null)
        {
            return;
        }
        if (room.status != Room.Status.FIGHTING)
        {
            return;
        }
        ////防作弊
        //if (Math.Abs(player.x - msg.x) > 5 ||
        //     Math.Abs(player.y - msg.y) > 5 ||
        //     Math.Abs(player.z - msg.z) > 5)
        //{
        //    Console.WriteLine("数据异常，玩家可能作弊,id:" + msg.id);
        //}
        //更新数据
        player.x = msg.x;
        player.y = msg.y;
        player.z = msg.z;
        player.ex = msg.ex;
        player.ey = msg.ey;
        player.ez = msg.ez;
        //广播
        msg.id = player.id;
        room.BroadCast(msg);
    }

    public static void MsgHit(ClientState state, MsgBase msgBase)
    {
        MsgHit msg = (MsgHit)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        Player targetPlayer = PlayerManager.GetPlayer(msg.targetID);
        if (targetPlayer == null)
        {
            return;
        }
        Room room = RoomManager.GetRoom(player.roomID);
        if (room == null)
        {
            return;
        }
        if (room.status != Room.Status.FIGHTING)
        {
            return;
        }
        if (player.id != msg.id)
        {
            return;
        }
        int damage = 10;
        targetPlayer.hp -= damage;
        //广播
        msg.damage = damage;
        msg.hp = targetPlayer.hp;
        msg.id = player.id;
        room.BroadCast(msg);
    }
}